﻿package  {
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Linear;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	
	public class MainMenu extends MovieClip{
		private var instructions:MovieClip;
		private var credits:MovieClip;
		
		private var bgm:Sound = new MenuBGM();
		private var bgmSoundChannel:SoundChannel = new SoundChannel();
		
		public function MainMenu() {
			stop();
			
			addEventListener(Event.ADDED_TO_STAGE, onAdded);
		}
		
		private function onAdded(e:Event):void{
			removeEventListener(Event.ADDED_TO_STAGE, onAdded);
			
			bgmSoundChannel = bgm.play();
			bgmSoundChannel.addEventListener(Event.SOUND_COMPLETE, loopSound);
			
			instructions = new Instructions();
			instructions.x = 400; instructions.y = 300;
			credits = new Credits();
			credits.x = 400; credits.y = 300;
			
			addMenuListeners();
			
		}
		
		private function loopSound(e:Event):void{
			bgmSoundChannel = bgm.play();
			bgmSoundChannel.addEventListener(Event.SOUND_COMPLETE, loopSound);
		}
		
		private function addMenuListeners():void{
			startButton.addEventListener(MouseEvent.CLICK, onStartClick);
			instructionsButton.addEventListener(MouseEvent.CLICK, onInstructionsClick);
			creditsButton.addEventListener(MouseEvent.CLICK, onCreditsClick);
		}
		
		private function removeMenuListeners():void{
			startButton.removeEventListener(MouseEvent.CLICK, onStartClick);
			instructionsButton.removeEventListener(MouseEvent.CLICK, onInstructionsClick);
			creditsButton.addEventListener(MouseEvent.CLICK, onCreditsClick);
		}
		
		private function onStartClick(e:MouseEvent):void{
			removeMenuListeners();
			gotoAndStop(2);
			
			//addDifficultyListeners();
			Settings.Difficulty = Constants.DIFFICULTY_NORMAL;
			Settings.CandlesLeft = Constants.STARTING_CANDLES;
			
			startGame();
		}
		
		private function onInstructionsClick(e:MouseEvent):void{
			addChild(instructions);
			
			instructions.addEventListener(MouseEvent.CLICK, onCloseInstructions);
		}
		
		private function onCloseInstructions(e:MouseEvent):void{
			instructions.removeEventListener(MouseEvent.CLICK, onCloseInstructions);
			
			Util.safeRemove(instructions);
		}
		
		private function onCreditsClick(e:MouseEvent):void{
			addChild(credits);
			
			credits.addEventListener(MouseEvent.CLICK, onCloseCredits);
		}
		
		private function onCloseCredits(e:MouseEvent):void{
			credits.removeEventListener(MouseEvent.CLICK, onCloseCredits);
			
			Util.safeRemove(credits);
		}
		
		private function addDifficultyListeners():void{
			//normalButton.addEventListener(MouseEvent.CLICK, onNormalClick);
		}
		
		private function removeDifficultyListeners():void{
			//normalButton.removeEventListener(MouseEvent.CLICK, onNormalClick);
		}
		
		private function onNormalClick(e:MouseEvent):void{
			Settings.Difficulty = Constants.DIFFICULTY_NORMAL;
			
			startGame();
		}
		
		private function startGame():void{
			removeDifficultyListeners();
			
			fadeToStart();
		}
		
		private function fadeToStart():void{
			var fade:MovieClip = new Fade();
			addChild(fade);
			TweenLite.from(fade, .5, {alpha:0, ease:Linear.easeNone, onComplete:switchToGame});
		}
		
		private function switchToGame():void{
			bgmSoundChannel.removeEventListener(Event.SOUND_COMPLETE, loopSound);
			bgmSoundChannel.stop();
			
			(this.root as Main).startGame();
			Util.safeRemove(this);
		}

	}
	
}
